
let list = [
    {
        id: 1,
        parentId: 0,
        name: "菜单1",
        children:[]
    },
    {
        id: 2,
        parentId: 1,
        name: "菜单1-1",
        children:[]
    },
    {
        id: 3,
        parentId: 1,
        name: "菜单1-2",
        children:[]
    },
    {
        id: 4,
        parentId: 2,
        name: "菜单1-1-1",
        children:[]
    },
]
console.log(add()[0].children[0].children[0])
 function add() {
    let menuList = [];
    list.map(item => {
        if (item.parentId == 0) {
            item.children = recriution(item, list);
            menuList.push(item);
        }
    })
    return menuList;
}

 function recriution(parent, list) {
    let children = [];
    list.map(item => {
        if (parent.id == item.parentId) {
            item.children=recriution(item,list);
            children.push(item);
        }
    })
    return children;
}